package math

import (
	"JVM/Jvmgo/GoToJVM/instructions/base"
	"JVM/Jvmgo/GoToJVM/runtimeData"
	"math"
)

/*
	这里是进行数学 取模 运算
*/

type IREM struct {
	base.NoOperandsInstruction
}
type LREM struct {
	base.NoOperandsInstruction
}
type FREM struct {
	base.NoOperandsInstruction
}
type DREM struct {
	base.NoOperandsInstruction
}

func (self IREM) Execute(frame *runtimeData.Frame) {
	stack := frame.OperandStack()
	val2 := stack.PopInt()
	val1 := stack.PopInt()
	if val2 == 0 {
		panic("java.lang.ArithmeticException: / by zero")
	}
	res := val1 % val2
	stack.PushInt(res)
}
func (self LREM) Execute(frame *runtimeData.Frame) {
	stack := frame.OperandStack()
	val2 := stack.PopLong()
	val1 := stack.PopLong()
	if val2 == 0 {
		panic("java.lang.ArithmeticException: / by zero")
	}
	res := val1 % val2
	stack.PushLong(res)
}
func (self FREM) Execute(frame *runtimeData.Frame) {
	stack := frame.OperandStack()
	val2 := stack.PopFloat()
	val1 := stack.PopFloat()
	res := float32(math.Mod(float64(val1), float64(val2)))
	stack.PushFloat(res)
}
func (self DREM) Execute(frame *runtimeData.Frame) {
	stack := frame.OperandStack()
	val2 := stack.PopDouble()
	val1 := stack.PopDouble()
	res := math.Mod(val1, val2)
	stack.PushDouble(res)
}
